package com.czy.tank.game;

import com.czy.tank.common.Dir;
import com.czy.tank.common.Group;
import com.czy.tank.config.PropertyMgr;
import com.czy.tank.game.Audio;
import com.czy.tank.game.Bullet;
import com.czy.tank.game.Explode;
import com.czy.tank.game.Tank;
import com.czy.tank.pattern.strategy.fire.FourFireStrategy;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/********************************************************************
 * @brief 主窗口
 * @version 0.1
 * @date 2022年08月30日 22:04
 * @author changziyang
 ********************************************************************/
public class TankFrame extends Frame {

    /*定义主界面 宽高*/
    static final int GAME_WIDTH = PropertyMgr.getInstance().getInt("gameWidth"),
            GAME_HEIGHT = PropertyMgr.getInstance().getInt("gameHeight");
    /*单例 创建主窗口*/
    public static TankFrame INSTANCE = new TankFrame();

    public static TankFrame getInstance(){
        return INSTANCE;
    }
    /*主战坦克*/
    Tank myTank = new Tank(400, 450, Dir.UP,this, Group.GOOD);
    /*定义子弹容器，发射时存入*/
    public List<Bullet> bullets =  new ArrayList<>();
    /*定义地方坦克容器*/
    public List<Tank> badTanks =  new ArrayList<>();
    /*定义爆炸容器*/
    public List<Explode> explodes = new ArrayList<>();

    private TankFrame() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("坦克大战-常紫阳");
        this.addKeyListener(new MyKeyListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        /*坦克图片切换*/
        changeTank();
    }

    /*************************************************
    * 方法描述:每隔1秒 坦克图片切换一次
    * @param
    * @return void
    * @author create: 人员:【changziyang】类型:【新增方法】日期:【2022/9/1 15:35】
    * @author modify:
    **************************************************/
    private void changeTank() {
        new Thread(()->{
            for (;;){
                try {
                    Thread.sleep(500);
                    if (myTank != null){
                        if (myTank.isFlag()){
                            myTank.setFlag(false);
                        }else {
                            myTank.setFlag(true);
                        }
                    }
                    for (int i = 0; i < badTanks.size(); i++) {
                        Tank tank = badTanks.get(i);
                        if (tank.isFlag()){
                            tank.setFlag(false);
                        }else {
                            tank.setFlag(true);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private Image offScreenImage = null;
    /*************************************************
     * 方法描述:解决刷新闪烁问题
     * @param g
     * @return void
     * @author create: 人员:【changziyang】类型:【新增方法】日期:【2022/8/30 22:10】
     * @author modify:
     **************************************************/
    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹数量:"+bullets.size(),10,50);
        g.drawString("敌方数量:"+badTanks.size(),10,70);
        g.drawString("爆炸数量:"+explodes.size(),10,90);
        g.setColor(color);
        /*主战坦克存活时，才会画出*/
        if (myTank != null){
            myTank.paint(g);
        }

        /*for i 循环中可以解决子弹消亡时产生的 java.util.ConcurrentModificationException*/
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
        for (int i = 0; i < bullets.size(); i++) {
            if (myTank != null){
                bullets.get(i).collideWith(myTank);
            }
            for (int j = 0; j < badTanks.size(); j++) {
                bullets.get(i).collideWith(badTanks.get(j));
            }
        }
        for (int i = 0; i < badTanks.size(); i++) {
            badTanks.get(i).paint(g);
        }
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }

    }

    class MyKeyListener extends KeyAdapter {

        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            /*播放坦克移动音效*/
            new Thread(()->new Audio("audio/tank_move.wav").play()).start();
            setMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_SPACE:
                    if (myTank != null){
                        myTank.fire();
                    }
                    break;
                default:
                    break;
            }
            setMainTankDir();

        }

        private void setMainTankDir() {
            if (myTank == null){
                return;
            }
            //save the old dir
            Dir dir = myTank.getDir();

            if (!bL && !bU && !bR && !bD) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (bL)
                    myTank.setDir(Dir.LEFT);
                if (bU)
                    myTank.setDir(Dir.UP);
                if (bR)
                    myTank.setDir(Dir.RIGHT);
                if (bD)
                    myTank.setDir(Dir.DOWN);

            }
        }
    }
}
